package hubol;

import static bogus.core.BogusCore.egl;

import java.awt.event.KeyEvent;
import java.util.ArrayList;

import owg.util.EntityBase;
import owg.util.InstanceList;
import owg.util.euclidian.Compass;
import owg.util.opengl.ColorF;
import world.Sound;
import bogus.core.CoreParams;
import bogus.minigame.MinigameBase;
import bogus.userInput.Input;

public abstract class HMinigame implements MinigameBase{
	public static final Sprite quit = new Sprite("-quit");
    public static final int width = 864;
    public static final int height = 486;
    
    public double viewX, viewY;

    public static HMinigame me;

    protected int quitTimer;
    
    protected InstanceList list;

    public HMinigame(){
	me = this;
    }

    @Override
    public void init() {
	this.list = new InstanceList();

	egl.modelviewMatrix().identity();
	egl.projectionMatrix().identity();
	egl.projectionMatrix().ortho((float)viewX, width + (float)viewX, height + (float)viewY, (float)viewY, -1, 1);

	egl.setColor(ColorF.WHITE);
	egl.setAlpha(1f);
	
	quit.setOrigin(Compass.NORTHWEST);
	initImpl();
    }

    public abstract void initImpl();

    @Override
    public CoreParams getCoreParams() {
        return new CoreParams(getFps());
    }
    
    public abstract int getFps();
    
    @Override
    public void step(){
	if (list != null){
	    list.step();
	}
    }
    
    public void completeMinigame(){
    	completeMinigameImpl();
    	returnToWorldMap();
    }
    
    public abstract void completeMinigameImpl();
    
    public void returnToWorldMap(){
    	//TODO quit to world map
    	System.exit(0);
    }
    
    protected void quitStep(){
    	if (Input.checkFrameKey(KeyEvent.VK_ESCAPE)){
    		Sound.play("-quittap");
    		if (quitTimer > 0)
    			returnToWorldMap();
    		else
    			quitTimer = getFps() * 2;
    	}
    	
    	quitTimer--;
    }

    @Override
    public void render(){
    	egl.modelviewMatrix().identity();
    	egl.projectionMatrix().identity();
    	egl.projectionMatrix().ortho((float)viewX, width + (float)viewX, height + (float)viewY, (float)viewY, -1, 1);

    	egl.setColor(ColorF.WHITE);
    	egl.setAlpha(1f);
    	
		if (list != null)
		    list.render();
    }
    
    protected void quitRender(){
    	if (quitTimer > 0)
    		quit.render(0, viewX, viewY + 438);
    }


    @Override
    public void dispose() {
		list = null;
	
		if (me == this){
		    me = null;
		    System.out.println("killed...");
		}
    }

    public ArrayList<Entity> getEntities(){
	ArrayList<Entity> output = new ArrayList<Entity>();
	for (EntityBase e : list.retrieve()){
	    if (Entity.class.isInstance(e))
		output.add((Entity)e);
	}
	return output;
    }

    public ArrayList<Entity> getEntitiesOfTag(int... tag){
	ArrayList<Entity> output = new ArrayList<Entity>();
	for (Entity e : getEntities()){
	    if (e.isOfTag(tag))
		output.add(e);
	}
	return output;
    }

    public boolean collidesWithTag(Entity subject, double x, double y, int... tag) {
	ArrayList<Entity> contestants = getEntitiesOfTag(tag);
	contestants.remove(subject);
	for (Entity e : contestants){
	    if (subject.collidesWith(e,x,y))
		return true;
	}
	return false;
    }

    public ArrayList<Entity> collidesWithTaggedEntites(Entity subject, double x, double y, int... tag) {
	ArrayList<Entity> output = new ArrayList<Entity>();

	ArrayList<Entity> contestants = getEntitiesOfTag(tag);
	contestants.remove(subject);
	for (Entity e : contestants){
	    if (subject.collidesWith(e,x,y))
		output.add(e);
	}
	return output;
    }

}
